package com.qqtech.qquan.pub.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.pub.enums.PubConst;
import com.qqtech.qquan.pub.enums.PubPlatformEnum;
import com.qqtech.qquan.pub.model.PubSysParam;
import com.qqtech.qquan.pub.model.PubUpgradeVersion;
import com.qqtech.qquan.pub.model.PubUpgradeVersionQuery;
import com.qqtech.qquan.pub.service.PubSysParamService;
import com.qqtech.qquan.pub.service.PubUpgradeVersionService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/pub/upgrade/version")
public class PubUpgradeVersionController extends BaseWebController<PubUpgradeVersion, PubUpgradeVersionQuery> {
	@Resource
	private PubUpgradeVersionService pubUpgradeVersionService;
	@Resource
	private PubSysParamService pubSysParamService;

	@Override
	protected BaseService<PubUpgradeVersion> getBaseService() {
		return pubUpgradeVersionService;
	}

	/**
	 * 根据客户端(内容)版本号获取对应的升级内容
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getByVersion")
	@ResponseBody
	public Result getByVersion(@RequestBody Map<String, String> map) {
		Result result = new Result();
		String toVersion = "";
		String updFile = "";
		String currVersion = map.get("currVersion");
		String platform = map.get("platform");
		String serverVersion = "1.1.0";
		PubSysParam pubSysParam = pubSysParamService.getByKey(PubConst.PARAMKEY_PUB_UPGRADE_LEASTUPDVERSION);
		if (pubSysParam != null && StringUtil.isNotBlank(pubSysParam.getValue())) {
			serverVersion = pubSysParam.getValue();
		}
		if (StringUtil.isNewVersion(currVersion, serverVersion)) {// 如果版本太低，则按最低版本统一升级
			PubSysParam pubSysParamVer = pubSysParamService.getByKey(PubConst.PARAMKEY_PUB_UPGRADE_UPDVERSION_ANDROID);
			PubSysParam pubSysParamFile = pubSysParamService.getByKey(PubConst.PARAMKEY_PUB_UPGRADE_UPDFILE_ANDROID);
			if (("" + PubPlatformEnum.IOS.getCode()).equals(platform)) {
				pubSysParamVer = pubSysParamService.getByKey(PubConst.PARAMKEY_PUB_UPGRADE_UPDVERSION_IOS);
				pubSysParamFile = pubSysParamService.getByKey(PubConst.PARAMKEY_PUB_UPGRADE_UPDFILE_IOS);
			}
			if (pubSysParamVer != null && StringUtil.isNotBlank(pubSysParamVer.getValue())) {
				toVersion = pubSysParamVer.getValue();
			}
			if (pubSysParamFile != null && StringUtil.isNotBlank(pubSysParamFile.getValue())) {
				updFile = pubSysParamFile.getValue();
			}
		} else {
			PubUpgradeVersion pubUpgradeVersion = pubUpgradeVersionService.getByCurrVersion(currVersion);
			if (pubUpgradeVersion == null) {
				result.addErr("");
				return result;
			}
			updFile = pubUpgradeVersion.getAndroidFile();
			if (("" + PubPlatformEnum.IOS.getCode()).equals(platform)) {
				updFile = pubUpgradeVersion.getIosFile();
			}
			toVersion = pubUpgradeVersion.getToVersion();
		}
		if (StringUtil.isBlank(updFile) || StringUtil.isBlank(toVersion)) {
			result.addErr("");
			return result;
		}
		result.put("updFile", updFile);
		result.put("toVersion", toVersion);
		return result;
	}
}
